import { useNurses, useNurseUser, useSys, useWeChat, } from '@/service';
import { useAuthStore, useNurseStore, useNursesStore } from '@/store';
import { login } from '@tarojs/taro';
import { useRouterPush } from '@/composables';
import { storeToRefs } from 'pinia';

const { resetAuthStore, setToken, setNurseUser, setOpenid } = useAuthStore()

const { setCompany } = useNurseStore()

const { token, openid } = storeToRefs(useAuthStore())


const {
    resetNursesAuthStore,
    setToken: setNurseToken,
    setNurseNurses,
    setOpenid: setNursesOpenid } = useNursesStore()

const { token: nurseToken, openid: nursesOpenid } = storeToRefs(useNursesStore())

const { userLogin, queryByOpenId, add } = useNurseUser();

const { login: nurseLogin, queryByOpenId: nurseQueryByOpenId } = useNurses();

const { getOpenId, getPhoneNumber } = useWeChat();

export async function useAuthLogin() {
    if (token.value) return

    const { error, success } = await userLogin({ openid: openid.value });
    !error && success && setToken(success)
}

export async function useGetAuthInfo() {
    const { error, success } = await queryByOpenId({ openid: openid.value });

    if (!error && success?.id) {
        setNurseUser({ ...success })
    } else if (!success?.id && openid.value) {
        await add({
            openid: openid.value, status: '1', type: '1',
        })
        const { error, success } = await queryByOpenId({ openid: openid.value });
        if (!error && success?.id) setNurseUser({ ...success })
    } else {
        resetAuthStore()
    }
    return success?.id
}


export async function useGetPhone(code: string) {

    resetAuthStore()

    const { code: openIdCode } = await login()

    const { error, success } = await getPhoneNumber(code, openIdCode);
    console.log(error, success, "error, success=======================")

    if (!error && success?.openid) {
        setOpenid(success?.openid)
        return true
    }
    return false
}

export async function useGetOpenid() {

    resetAuthStore()

    const { code } = await login()
    console.log('error', code)
    const { error, success } = await getOpenId(code);
    console.log(error, success)
    if (!error && success?.openid) {
        setOpenid(success?.openid)
        // setNursesOpenid(success?.openid)
        // type == '1' && setOpenid(success?.openid)
        // type == '2' && setNursesOpenid(success?.openid)
        return true
    }
    return false
}



export async function useLogout() {
    const { toLogin } = useRouterPush()
    const { logOut } = useSys()
    logOut()
    resetAuthStore()
    resetNursesAuthStore()
    toLogin()
}

export async function useGlobal() {
    const { getCompany } = useSys()
    const { error, success } = await getCompany()
    if (!error && success) setCompany({ ...success?.list[0] })
}


export async function useNurseAuthLogin() {
    if (nurseToken.value) return false
    const { error, success } = await nurseLogin({ openid: nursesOpenid.value });
    if (!error && success) {
        setNurseToken(success)
        return true
    }
    return false
}

export async function useNurseGetAuthInfo() {
    const { error, success } = await nurseQueryByOpenId({ openid: nursesOpenid.value });

    if (!error && success?.id) {
        setNurseNurses({ ...success })
    } else {
        resetNursesAuthStore()
    }
    return success?.id
}



